package com.hnkywl.sts.action.kcgl;

import com.hnkywl.sts.dao.kcgl.KcpdDao;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.xsgl.Kcpd;
import com.hnkywl.sts.entity.xsgl.Kh;
import com.hnkywl.sts.entity.xsgl.Pdmx;
import com.hnkywl.sts.service.kcgl.KcpdService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.util.POIUtil;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.Region;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-25
 * Time: 下午2:41
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/kcgl/kcpd")
public class KcpdAction extends SimpleActionSupport {
    private KcpdService kcpdService;
    private KcpdDao kcpdDao;
    //库存报价
    private Kcpd kcpd;
    //库存报价Id
    private Long kcpdId;
    //盘点人
    private Long pdrId;
    //审核人
    private Long shryId;
    //仓库Id
    private Long ckId;
    //仓库名称
    private String ckMc;
    //盘点明细
    private Pdmx pdmx;
    //卡号
    private Kh kh;
    //系统时间
    private Date sysdate;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows = 10;
    //远程ajax验证的值
    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    @Action("kcpd")
    public String kcpd() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //库存盘点的查询分页显示
    @Action("kcpdList")
    public void kcpdList() {
        this.renderJson(this.kcpdService.findKcpdList(ckId, ckMc, page, rows));
    }

    @Action("kcpd_content")
    public String kcpdContent() {
        if (kcpdId != null) {
            kcpd = kcpdService.findKcpdDetail(kcpdId);
        } else {
            sysdate = new Date();
        }
        return SUCCESS;
    }

    //添加库存盘点
    @Action("add_kcpd")
    public void addKcpd() {
        try {
            this.kcpdService.saveKcpd(kcpd);
            this.kcpdService.createPdmx(kcpd);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加库存盘点成功！\", \"operationType\":\"add\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //删除库存盘点
    @Action("delete_kcpd")
    public void deleteKcpd() {
        try {
            if (this.kcpdService.findKcpdDetail(kcpdId).getSfsh()) {
                renderText("{\"statusCode\":\"200\", \"message\":\"此盘点单已经审核无法删除！\", \"operationType\":\"delete\"}");
                return;
            }
            this.kcpdService.deleteKcpd(kcpdId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除库存盘点成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }   //sh_kcpd

    //删除库存盘点
    @Action("sh_kcpd")
    public void shKcpd() {
        try {
            if (this.kcpdService.findKcpdDetail(kcpdId).getSfsh()) {
                renderText("{\"statusCode\":\"200\", \"message\":\"此盘点单已经审核无法审核！\", \"operationType\":\"delete\"}");
                return;
            }
            this.kcpdService.shKcpd(kcpdId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除库存盘点成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //实盘
    @Action("kcpd_detail")
    public String kcpdDetail() {
        if (kcpdId != null) {
            kcpd = kcpdService.findKcpdDetail(kcpdId);
        }
        return SUCCESS;
    }

    //库存盘点明细
    @Action("kcpd_pdmx")
    public void kcpdPdmx() {
        this.renderJson(this.kcpdService.findPdmxList(kcpdId));
    }

    //保存库存盘点明细
    @Action("sava_pdmx")
    public void savaPdmx() {
        try {
            if (this.kcpdService.findKcpdDetail(kcpd.getId()).getSfsh()) {
                renderText("{\"statusCode\":\"200\", \"message\":\"此单据已经实盘审核，无法保存实盘信息！\", \"operationType\":\"add\"}");
            }
            this.kcpdService.savePdmx(kcpd);
            renderText("{\"statusCode\":\"200\", \"message\":\"实盘成功！\", \"operationType\":\"add\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 远程ajax验证
     */
    @Action("ajax_validate")
    public void ajaxValidate() {
        if (!kcpdService.ajaxValidate(kcpdId, validateField, validateVal))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    @Action("kcpd_export")
    public String kcpdExport() {
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        List<Map> kcpdList = this.kcpdDao.findPdmxList(kcpdId);
        //第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("库存盘点");
        POIUtil poiUtil = new POIUtil(wb);
        //设置列宽
        sheet.setDefaultRowHeightInPoints(15);
        sheet.setDefaultColumnWidth((short) 17);
        sheet.setColumnWidth((short) 0, (short) (11 * 512));
        sheet.setColumnWidth((short) 1, (short) (9 * 512));
        sheet.setColumnWidth((short) 2, (short) (9 * 512));
        sheet.setColumnWidth((short) 3, (short) (15 * 512));
        sheet.setColumnWidth((short) 4, (short) (15 * 512));
        sheet.setColumnWidth((short) 5, (short) (6 * 512));
        sheet.setColumnWidth((short) 6, (short) (6 * 512));
        sheet.setColumnWidth((short) 7, (short) (10 * 512));
        sheet.setColumnWidth((short) 8, (short) (6 * 512));
        sheet.setColumnWidth((short) 9, (short) (6 * 512));
        sheet.setColumnWidth((short) 10, (short) (6 * 512));
        sheet.setColumnWidth((short) 11, (short) (10 * 512));
        sheet.setColumnWidth((short) 12, (short) (10 * 512));
        sheet.setColumnWidth((short) 13, (short) (10 * 512));
        //第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        row.setHeightInPoints(36);
        //第四步，创建单元格，并设置值表头  设置表头居中
        HSSFCellStyle titleStyle = poiUtil.createTitleHSSFCellStyle(wb);
        HSSFCell titleCell = row.createCell((short) 0);
        titleCell.setCellStyle(titleStyle);
        titleCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        titleCell.setCellValue("库存盘点导出汇总表");
        // 四个参数分别是：起始行，起始列，结束行，结束列
        sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 16));
        row = sheet.createRow((int) 1);
        row.setHeightInPoints(21);
        //生成第一行数据
        HSSFCell kcbm = row.createCell((short) 0);
        kcbm.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        kcbm.setEncoding(HSSFCell.ENCODING_UTF_16);
        kcbm.setCellValue("卡号");

        HSSFCell wzbm = row.createCell((short) 1);
        wzbm.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        wzbm.setEncoding(HSSFCell.ENCODING_UTF_16);
        wzbm.setCellValue("物资编码");

        HSSFCell wzname = row.createCell((short) 2);
        wzname.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        wzname.setEncoding(HSSFCell.ENCODING_UTF_16);
        wzname.setCellValue("物资名称");

        HSSFCell wzgg = row.createCell((short) 3);
        wzgg.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        wzgg.setEncoding(HSSFCell.ENCODING_UTF_16);
        wzgg.setCellValue("规格型号");

        HSSFCell cz = row.createCell((short) 4);
        cz.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        cz.setEncoding(HSSFCell.ENCODING_UTF_16);
        cz.setCellValue("材质");

        HSSFCell cd = row.createCell((short) 5);
        cd.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        cd.setEncoding(HSSFCell.ENCODING_UTF_16);
        cd.setCellValue("产地");

        HSSFCell dw = row.createCell((short) 6);
        dw.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        dw.setEncoding(HSSFCell.ENCODING_UTF_16);
        dw.setCellValue("单位");

        HSSFCell wsdj = row.createCell((short) 7);
        wsdj.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        wsdj.setEncoding(HSSFCell.ENCODING_UTF_16);
        wsdj.setCellValue("无税单价(元)");

        HSSFCell zmsl = row.createCell((short) 8);
        zmsl.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        zmsl.setEncoding(HSSFCell.ENCODING_UTF_16);
        zmsl.setCellValue("账面数量");

        HSSFCell spsl = row.createCell((short) 9);
        spsl.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        spsl.setEncoding(HSSFCell.ENCODING_UTF_16);
        spsl.setCellValue("实盘数量");

        HSSFCell yksl = row.createCell((short) 10);
        yksl.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        yksl.setEncoding(HSSFCell.ENCODING_UTF_16);
        yksl.setCellValue("盈亏数量");

        HSSFCell ykje = row.createCell((short) 11);
        ykje.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        ykje.setEncoding(HSSFCell.ENCODING_UTF_16);
        ykje.setCellValue("盈亏总金额(元)");

        //表体部分
        for (int i = 2; i < kcpdList.size() + 2; i++) {
            row = sheet.createRow(i);
            row.setHeightInPoints(19);

            kcbm = row.createCell((short) 0);
            kcbm.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            kcbm.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != kcpdList.get(i - 2).get("kh.khbm")) {
                kcbm.setCellValue(kcpdList.get(i - 2).get("kh.khbm").toString());
            }

            wzbm = row.createCell((short) 1);
            wzbm.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            wzbm.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != kcpdList.get(i - 2).get("wzgg.code")) {
                wzbm.setCellValue(kcpdList.get(i - 2).get("wzgg.code").toString());
            }

            wzname = row.createCell((short) 2);
            wzname.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            wzname.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != kcpdList.get(i - 2).get("wzgg.name")) {
                wzname.setCellValue(kcpdList.get(i - 2).get("wzgg.name").toString());
            }

            wzgg = row.createCell((short) 3);
            wzgg.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            wzgg.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != kcpdList.get(i - 2).get("wzgg.ggxh")) {
                wzgg.setCellValue(kcpdList.get(i - 2).get("wzgg.ggxh").toString());
            }

            cz = row.createCell((short) 4);
            cz.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            cz.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != kcpdList.get(i - 2).get("wzgg.cz")) {
                cz.setCellValue(kcpdList.get(i - 2).get("wzgg.cz").toString());
            }

            cd = row.createCell((short) 5);
            cd.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            cd.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != kcpdList.get(i - 2).get("cd.z")) {
                cd.setCellValue(kcpdList.get(i - 2).get("cd.z").toString());
            }

            dw = row.createCell((short) 6);
            dw.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            dw.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != kcpdList.get(i - 2).get("wzgg.dw")) {
                dw.setCellValue(kcpdList.get(i - 2).get("wzgg.dw").toString());
            }

            wsdj = row.createCell((short) 7);
            wsdj.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            wsdj.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != kcpdList.get(i - 2).get("wsdj")) {
                BigDecimal bigDecimal_wsdj = new BigDecimal(kcpdList.get(i - 2).get("wsdj").toString()).setScale(3, BigDecimal.ROUND_HALF_UP);
                wsdj.setCellValue(bigDecimal_wsdj.doubleValue());
            }

            zmsl = row.createCell((short) 8);
            zmsl.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            zmsl.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != kcpdList.get(i - 2).get("zmsl")) {
                BigDecimal bigDecimal_zmsl = new BigDecimal(kcpdList.get(i - 2).get("zmsl").toString()).setScale(3, BigDecimal.ROUND_HALF_UP);
                zmsl.setCellValue(bigDecimal_zmsl.doubleValue());
            }

            spsl = row.createCell((short) 9);
            spsl.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            spsl.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != kcpdList.get(i - 2).get("spsl")) {
                BigDecimal bigDecimal_spsl = new BigDecimal(kcpdList.get(i - 2).get("spsl").toString()).setScale(3, BigDecimal.ROUND_HALF_UP);
                spsl.setCellValue(bigDecimal_spsl.doubleValue());
            }

            yksl = row.createCell((short) 10);
            yksl.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            yksl.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != kcpdList.get(i - 2).get("yksl")) {
                BigDecimal bigDecimal_yksl = new BigDecimal(kcpdList.get(i - 2).get("yksl").toString()).setScale(3, BigDecimal.ROUND_HALF_UP);
                yksl.setCellValue(bigDecimal_yksl.doubleValue());
            }

            ykje = row.createCell((short) 11);
            ykje.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            ykje.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != kcpdList.get(i - 2).get("ykje")) {
                BigDecimal bigDecimal_ykje = new BigDecimal(kcpdList.get(i - 2).get("ykje").toString()).setScale(3, BigDecimal.ROUND_HALF_UP);
                ykje.setCellValue(bigDecimal_ykje.doubleValue());
            }
        }

        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("库存盘点汇总表.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }

    //==============getter and setter ===========
    public Long getPdrId() {
        return pdrId;
    }

    public void setPdrId(Long pdrId) {
        this.pdrId = pdrId;
    }

    public Long getShryId() {
        return shryId;
    }

    public void setShryId(Long shryId) {
        this.shryId = shryId;
    }

    public Kcpd getKcpd() {
        return kcpd;
    }

    public void setKcpd(Kcpd kcpd) {
        this.kcpd = kcpd;
    }

    public Long getKcpdId() {
        return kcpdId;
    }

    public void setKcpdId(Long kcpdId) {
        this.kcpdId = kcpdId;
    }

    public Date getSysdate() {
        return sysdate;
    }

    public void setSysdate(Date sysdate) {
        this.sysdate = sysdate;
    }

    public Long getCkId() {
        return ckId;
    }

    public void setCkId(Long ckId) {
        this.ckId = ckId;
    }

    public String getCkMc() {
        return ckMc;
    }

    public void setCkMc(String ckMc) {
        this.ckMc = ckMc;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Pdmx getPdmx() {
        return pdmx;
    }

    public void setPdmx(Pdmx pdmx) {
        this.pdmx = pdmx;
    }

    public Kh getKh() {
        return kh;
    }

    public void setKh(Kh kh) {
        this.kh = kh;
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public String getValidateField() {
        return validateField;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setKcpdService(KcpdService kcpdService) {
        this.kcpdService = kcpdService;
    }

    @Autowired
    public void setKcpdDao(KcpdDao kcpdDao) {
        this.kcpdDao = kcpdDao;
    }
}
